import axios from "axios";
import { useStore } from "../pinia/index";

let baseURL = "";
if (location.href.includes("huruqing")) {
  baseURL = "http://43.136.85.99:7777";
} else {
  baseURL = "http://localhost:7777";
  // baseURL = "http://43.136.85.99:7777";
}

const service = axios.create({
  baseURL,
  timeout: 30000,
});

// request拦截器
service.interceptors.request.use(
  (config) => {
    const store = useStore();
    console.log(store)
    config.headers["user-token"] = store.userId;
    return config;
  },
  (error) => {
    alert("请求拦截器-报错了");
    return Promise.reject(error);
  }
);

// response 拦截器
service.interceptors.response.use(
  (response) => {
    const res = response.data;
    if (res.code == 666) {
      return res;
    } else {
      alert("返回数据-报错了");
      return Promise.reject(res.msg);
    }
  },
  (error) => {
    alert("请求没有成功发出");
    return Promise.reject(error.message);
  }
);


export let get = service.get;
export let post = service.post;
export default service;
